package com.frxuexi.park.viewmodel

import com.frxuexi.lib_base.http.RequestType
import com.frxuexi.lib_base.mvi.BaseViewModel
import com.frxuexi.lib_base.room.ParkDao
import com.frxuexi.park.intent.ParkPreIntent
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.flow
import javax.inject.Inject

@HiltViewModel
class ParkPreViewModel @Inject constructor(val parkDao: ParkDao):BaseViewModel<ParkPreIntent>(){
    override fun handleIntent(it: ParkPreIntent) {
        when(it){
            is ParkPreIntent.addPark->roomRequest(flow { emit(parkDao.addPark(it.entity)) },RequestType.ADD)
            is ParkPreIntent.deletePark->roomRequest(flow { emit(parkDao.deletePark(it.entity)) },RequestType.DELETE)
            is ParkPreIntent.updatePark->roomRequest(flow { emit(parkDao.update(it.entity)) },RequestType.UPDATE)
            is ParkPreIntent.getParksByDate->roomRequest(flow { emit(parkDao.getParksByDate(it.date)) })
            is ParkPreIntent.getCountByDate->roomRequest(flow { emit(parkDao.getCountByDate(it.date)) })
            is ParkPreIntent.getParksByDateOrNum->roomRequest(flow { emit(parkDao.getParksByDateOrNum(it.para)) })
            is ParkPreIntent.getParksById->roomRequest(flow { emit(parkDao.getParksById(it.id.toLong())) },RequestType.SELECT_ITEM)
            is ParkPreIntent.getParks->roomRequest(flow { emit(parkDao.getParks(it.size,it.offset)) })
        }
    }
}